package com.cnhis.cloudhealth.clinical.osworks.commonsql.bo;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OperationHocusDao;
import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OperationJobDao;
import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OsBckMedicationDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.MapUtils;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.Xml2JsonUtil;
import com.cnhis.cloudhealth.commons.vo.exception.BaseException;

@Service
public class OperationJobBo {

	@Resource
	private OperationJobDao operationJobDao;

	/**
	 * 手术安排查看手术间情况
	 * 张清华
	 * 2017-03-02 
	 */
	public ModelVo get16765(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.get16765(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 手术安排查看主刀医师状态
	 * 张清华
	 * 2017-03-02 
	 */
	public ModelVo get16767(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.get16767(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 新增手术间
	 * @param map
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	public ModelVo insertBee1(Map<String, Object> map) {
		Map mapxml = null;
		ModelVo mv = new ModelVo();
		try {
			String Doc=map.get("Doc").toString();
			mapxml = Xml2JsonUtil.xmlElements(map.get("Doc").toString().toLowerCase());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new BaseException("解析XML文档时发生错误，错误号: %d.");
		}	
		try{
			List tmpBEE1 = MapUtils.getValueByNameLs(mapxml, "bee1");
			if(tmpBEE1.size()<=0){
				//throw new BaseException("没有找到要提交的发药内容，请重新后重试！");
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("提交错误，手术室信息为空！");
				return mv;
			}
			operationJobDao.deleteBee1(map);
			map.put("list", tmpBEE1);
			operationJobDao.insertbatchBee1(map);
			/*for (int i = 0; i < tmpBEE1.size(); i++) {
				Map mapList = (Map) tmpBEE1.get(i);
				operationJobDao.insertBee1(mapList);
			}*/
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 查看手术间最大ID
	 * @return
	 */
	public ModelVo selectMaxBee01() {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.selectMaxBee01();
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 查看手术团队
	 * @return
	 */
	public ModelVo selectBmn1() {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.selectBmn1();
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 查看手术团队从表
	 * @param map
	 * @return
	 */
	public ModelVo selectBmo1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.selectBmo1(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 查看手术团队主表最大ID
	 * @return
	 */
	public ModelVo selectMaxBmn01() {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.selectMaxBmn01();
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 删除手术团队
	 * @param map
	 * @return
	 */
	public ModelVo deleteDmn01(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			operationJobDao.deleteDmn01(map);
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 查看员工
	 * @param map
	 * @return
	 */
	public ModelVo selectBce1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.selectBce1(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 查看手术安排人员
	 * @param map
	 * @return
	 */
	public ModelVo selectBmi1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.selectBmi1(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 手术医师安排新增
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public ModelVo insertBmi1(Map<String, Object> map) {
		Map mapxml = null;
		ModelVo mv = new ModelVo();
		try {
			mapxml = Xml2JsonUtil.xmlElements(map.get("Doc").toString().toLowerCase());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new BaseException("解析XML文档时发生错误，错误号: %d.");
		}	
		try{
			List tmpbmi1 = MapUtils.getValueByNameLs(mapxml, "bmi1");
			if(tmpbmi1.size()<=0){
				//throw new BaseException("没有找到要提交的发药内容，请重新后重试！");
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("手术工作人员信息为空,保存失败,请重新填写工作人员信息！");
				return mv;
			}
			operationJobDao.deleteBmi1();
			map.put("list", tmpbmi1);
			operationJobDao.insertbatchBmi1(map);
			/*for (int i = 0; i < tmpbmi1.size(); i++) {
				Map mapList = (Map) tmpbmi1.get(i);
				operationJobDao.insertBmi1(mapList);
			}*/
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 手术团队新增
	 * @param map
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unused" })
	public ModelVo insertBmn1Bmo1(Map<String, Object> map) {
		Map mapxml = null;
		ModelVo mv = new ModelVo();
		try {
			mapxml = Xml2JsonUtil.xmlElements(map.get("Doc").toString().toLowerCase());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new BaseException("解析XML文档时发生错误，错误号: %d.");
		}	
		try{
			List tmpbmn1 = MapUtils.getValueByNameLs(mapxml, "bmn1");
			if(tmpbmn1.size()<=0){
				//throw new BaseException("没有找到要提交的发药内容，请重新后重试！");
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("提交错误信息为空！");
				return mv;
			}
			List tmpbmo1 = MapUtils.getValueByNameLs(mapxml, "bmo1");
			if(tmpbmo1.size()<=0){
				//throw new BaseException("没有找到要提交的发药内容，请重新后重试！");
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("提交错误信息为空！");
				return mv;
			}
			operationJobDao.deleteBMN1BMO1();
			for (int i = 0; i < tmpbmn1.size(); i++) {
				Map mapList = (Map) tmpbmn1.get(i);
				operationJobDao.insertBMN1(mapList);
			}
			map.put("list", tmpbmo1);
			operationJobDao.insertbatchBmo1(map);
			/*for (int i = 0; i < tmpbmo1.size(); i++) {
				Map mapList = (Map) tmpbmo1.get(i);
				operationJobDao.insertBMO1(mapList);
			}*/
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 手术安排撤销
	 * @param map
	 * @return
	 */
	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	public ModelVo updateOperationSchedule(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			//删除病人医生
			operationJobDao.deleteVAX1(map);
			//判断如果是安排手术
			if(GlobalVariable.IntegerIsNull(map.get("vat04"))==2){
				Map mapxml = null;
				try {
					mapxml = Xml2JsonUtil.xmlElements(map.get("Doc").toString().toLowerCase());
				} catch (Exception ex) {
					ex.printStackTrace();
					throw new BaseException("解析XML文档时发生错误，错误号: %d.");
				}
				List tmpvax1 = MapUtils.getValueByNameLs(mapxml, "vax1");
				if(tmpvax1.size() > 0){
					String VAX1MAX = operationJobDao.selectVAX1MAX();
					for(int i = 0; i < tmpvax1.size(); i++){
						Map mapList = (Map) tmpvax1.get(i);
						mapList.put("vax01", VAX1MAX);
						mapList.put("i", i);
						operationJobDao.insertVAX1(mapList);
					}
				}
			}
			operationJobDao.updateOperationSchedule(map);
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo get16798(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.get16798(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo selectBmo1List() {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.selectBmo1List();
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	@SuppressWarnings("rawtypes")
	public ModelVo selectMaxVdg10(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationJobDao.selectMaxVdg10(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
}
